package com.hamster.crm.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hamster.crm.pojo.User;
import com.hamster.crm.pojo.dto.DepartmentUserList;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Tarry
 * @create 2019/12/28 8:37
 */
@Repository
@Mapper
public interface UserMapper extends BaseMapper<User> {
   // @Select("select permission_id from tb_user_role A,tb_role_permission B where A.id = #{id} and B.role_id = A.role_id")
    @Select("select permission_id from tb_user_role A,tb_role_permission B where A.user_id = #{id} and B.role_id = A.role_id")
    List<String> queryPermissionIdByUserId(String id);
    //    获取用户列表
    @Select("select id,phone,name,gender,department,position,state from tb_user")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "phone",column = "phone"),
            @Result(property = "name",column = "name"),
            @Result(property = "gender",column = "gender"),
            @Result(property = "deptStr",column = "department",one = @One(select = "com.hamster.crm.mapper.UserMapper.queryDeptNameById")),
            @Result(property = "position",column = "position"),
            @Result(property = "state",column = "state")
    })
    List<User> queryUserList();

    /**
     * 根据角色查询 user 列表
     * @param roleId
     * @return
     */
    @Select("select A.id,A.phone,A.name,A.department,A.position from tb_user A,tb_user_role B where A.id = B.user_id and B.role_id = #{roleId}")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "name",column = "name"),
            @Result(property = "phone",column = "phone"),
            @Result(property = "department",column = "department",one = @One(select = "com.hamster.crm.mapper.UserMapper.queryDeptNameById")),
            @Result(property = "position",column = "position")
    })
    List<User> listUserByRole(@Param("roleId") String roleId);

    @Select("select title from dictionary_department where id =#{id}")
    String queryDeptNameById(@Param("id") String id);

    /**
     * 查询 用户下拉列表，方便关联角色
     * @return
     */
    @Select("select * from dictionary_department")
    @Results({
            @Result(property = "value",column = "id",id = true),
            @Result(property = "label",column = "title"),
            @Result(property = "children",column = "id",many = @Many(select = "com.hamster.crm.mapper.UserMapper.listUsersSelectChildren")),
    })
    List<DepartmentUserList> listUsersSelect();

    /**
     * 上面查询的子查询
     * @return
     */
    @Select("select * from tb_user where department = #{id}")
    @Results({
            @Result(property = "value",column = "id",id =true),
            @Result(property = "label",column = "name")
    })
    DepartmentUserList listUsersSelectChildren(@Param("id") String id);
}
